package com.bs.visitservice.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.Complain;
import com.bs.visitservice.entity.OrderInfo;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.ComplainService;
import com.bs.visitservice.service.OrderInfoService;
import com.bs.visitservice.service.SellerService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理投诉信息
 */
@RestController
@RequestMapping("/admin")
public class ComplainHanleController {

    @Autowired
    private ComplainService complainService;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private SellerService sellerService;
    /**
     * 查询用户投诉信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryComplains",method = RequestMethod.POST)
    public Result queryComplains(@RequestBody Map cond){
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<Complain> wrapper = new LambdaQueryWrapper();
        wrapper.orderByDesc(Complain::getDataTime);
        page  = complainService.pageMaps(page,wrapper);
        List<Map<String, Object>> res  = page.getRecords();
        res.forEach(map->{
            Long orderNo = (Long)map.get("orderNo");
            OrderInfo oi = orderInfoService.getById(orderNo);
            if(oi != null){
                map.put("orderNo",oi.getOrderNo());
                map.put("sellerNo",oi.getSellerNo());
            }
        });
        ResultUtil.transfort(res,getDictMap(),true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }

    /**
     * 处理投诉信息 成功1 失败2 0审核中
     * @param cond
     * @return
     */
    @Transactional
    @RequestMapping("/handleComplain")
    public Result handleComplain(@RequestBody Map cond){
        Complain complain = (Complain)ResultUtil.transObject(cond,Complain.class);
        if(complain.getStatus() == 1){
            Complain ct = complainService.getById(complain.getNo());
            //投诉成功 扣除商家店铺信誉值 暂时设定扣除5分
            OrderInfo orderInfo = orderInfoService.getById(ct.getOrderNo());
            Seller seller = sellerService.getById(orderInfo.getSellerNo());
            LambdaUpdateWrapper<Seller> wrapperSeller = new LambdaUpdateWrapper<Seller>();
            wrapperSeller.set(Seller::getCreditPoint,seller.getCreditPoint()-5 ).eq(Seller::getSellerNo,seller.getSellerNo());
            sellerService.update(wrapperSeller);
        }
        LambdaUpdateWrapper<Complain> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Complain::getStatus,complain.getStatus()).eq(Complain::getNo,complain.getNo());
        complainService.update(wrapper);
        return Result.success(null);
    }

    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("complainType","complainType");
        dictMap.put("status","complainStatus");
        return dictMap;
    }

}
